//================v2要点总结===============
/**
 * 1. 循环迭代
 * 2. 注意避免死循环，返回数组的最后一个
 */
/**
 * Definition for singly-linked list.
 *  */

// eslint-disable-next-line no-unused-vars
function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val)
    this.next = (next === undefined ? null : next)
}

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function (head) {
    if (!head || !head.next) {
        return head
    }
    let arr = []
    while (head) {
        arr.push(head)
        head = head.next
    }

    for (let i = arr.length - 1; i - 1 >= 0; i--) {
        arr[i].next = arr[i - 1]
    }
    // 尾结点next赋空
    arr[0].next = null
    return arr[arr.length - 1]
};

console.log(reverseList)